package com.tengju.bff.interfaces.covercharge.service.bestsign.impl.auth;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.tengju.bff.interfaces.covercharge.constant.ServiceException;
import com.tengju.bff.interfaces.covercharge.entity.vo.BusinessLicenseOcrResult;
import com.tengju.bff.interfaces.covercharge.entity.vo.Identity4Req;
import com.tengju.bff.interfaces.covercharge.entity.vo.SmsVerificationReq;
import com.tengju.bff.interfaces.covercharge.entity.vo.SmsVerificationVerifyReq;
import com.tengju.bff.interfaces.covercharge.service.bestsign.BestSignClient;
import com.tengju.bff.interfaces.covercharge.service.bestsign.constant.BestSignBizReqUrlEnum;
import com.tengju.bff.interfaces.covercharge.service.bestsign.convert.BestSignAuthConverter;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@Service
public class BestSignAuthService {

    @Resource
    private BestSignClient bestSignClient;


    /**
     * 个人手机号三要素获取
     */
    public String doIdentity3Auth(String bizId, SmsVerificationReq req) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignAuthConverter.packageIdentity3AuthReq(req,bestSignClient.getAccount());
            log.info("个人手机号三要素及验证码入参：{}", bizMap.toString());
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_100, bizMap, bizId);
            log.info("个人手机号三要素及验证码返回结果：result{},mobile{}", result,req.getMobile());
            log.info("短信验证码发送成功,mobile{}", req.getMobile());
        } catch (ServiceException se) {
            throw se;
        } catch (Exception e) {
            log.error("个人手机号三要素及验证码获取异常,异常信息:", e);
            throw new ServiceException("个人手机号三要素及验证码获取异常");
        }
        //结果检查
        String data = bestSignClient.checkResponse(result);
        log.info("个人手机号三要素及验证码获取：{}", result);
        return data;
    }

    /**
     * 个人手机号三要素校验
     */
    public String doIdentity3AuthVerify(String bizId, SmsVerificationVerifyReq req) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignAuthConverter.packageIdentity3AuthVerifyReq(req);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_101, bizMap, bizId);
        } catch (ServiceException se) {
            throw se;
        } catch (Exception e) {
            log.error("个人手机号三要素校验异常,异常信息:", e);
            throw new ServiceException("个人手机号三要素校验异常");
        }
        //结果检查
        String data = bestSignClient.checkResponse(result);
        return data;
    }

    /**
     * 营业执照OCR识别
     *
     * @param image 企业营业执照图片（base64）
     * @return 结果
     */
    public void businessLicenseOCR(String image) {
        Map result = null;
        try {
            String bizId = String.valueOf(IdWorker.getId());
            Map<String, Object> bizMap = BestSignAuthConverter.businessLicenseOCR(image);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_102, bizMap, bizId);
            JSONObject object = JSONObject.fromObject(result);
            if (result.get("errno").equals(0)) {
                BusinessLicenseOcrResult businessLicenseOcrResult = new BusinessLicenseOcrResult();
                if (object.getJSONObject("data").containsKey("regCode")) {
                    businessLicenseOcrResult.setRegCode(object.getJSONObject("data").getString("regCode"));
                }
                if (object.getJSONObject("data").containsKey("name")) {
                    businessLicenseOcrResult.setRegCode(object.getJSONObject("data").getString("name"));
                }
                if (object.getJSONObject("data").containsKey("legalPerson")) {
                    businessLicenseOcrResult.setRegCode(object.getJSONObject("data").getString("legalPerson"));
                }
                if (object.getJSONObject("data").containsKey("address")) {
                    businessLicenseOcrResult.setRegCode(object.getJSONObject("data").getString("address"));
                }
                if (object.getJSONObject("data").containsKey("establishDate")) {
                    businessLicenseOcrResult.setRegCode(object.getJSONObject("data").getString("establishDate"));
                }
                if (object.getJSONObject("data").containsKey("validPeriod")) {
                    businessLicenseOcrResult.setRegCode(object.getJSONObject("data").getString("validPeriod"));
                }
                if (object.getJSONObject("data").containsKey("capital")) {
                    businessLicenseOcrResult.setRegCode(object.getJSONObject("data").getString("capital"));
                }
                if (object.getJSONObject("data").containsKey("business")) {
                    businessLicenseOcrResult.setRegCode(object.getJSONObject("data").getString("business"));
                }
            }
            log.info("=========={}", result);
        } catch (ServiceException se) {
            throw se;
        } catch (Exception e) {
            log.error("营业执照OCR识别异常,异常信息:", e);
            throw new ServiceException("营业执照OCR识别异常");
        }
    }

    /**
     * 企业四要素验证
     *
     * @param identity4Req
     * @return 结果
     */
    public String identity4(Identity4Req identity4Req) {
        Map result = null;
        try {
            String bizId = String.valueOf(IdWorker.getId());
            Map<String, Object> bizMap = BestSignAuthConverter.identity4(identity4Req);
            log.info("企业验证入参:{}", bizMap.toString());
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_103, bizMap, bizId);
            String data = bestSignClient.checkResponse(result);
            return data;
        } catch (ServiceException se) {
            throw se;
        } catch (Exception e) {
            log.error("企业四要素验证异常,异常信息:", e);
            throw new ServiceException("企业四要素验证异常");
        }
    }
}
